<?php
/**
 +-----------------------------------------------------------------------+
 | dcFlickr - a Flickr plugin for Dotclear 2                             |
 | Copyright (C) 2008 - France                                           |
 |                                                                       |
 | This program is free software: you can redistribute it and/or modify  |
 | it under the terms of the GNU General Public License as published by  |
 | the Free Software Foundation, version 2 of the License.               |
 |                                                                       |
 | This program is distributed in the hope that it will be useful,       |
 | but WITHOUT ANY WARRANTY; without even the implied warranty of        |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
 | GNU General Public License for more details.                          |
 |                                                                       |
 | You should have received a copy of the GNU General Public License     |
 | along with this program.  If not, see <http://www.gnu.org/licenses/>. |
 |                                                                       |
 +-----------------------------------------------------------------------+
 |  Author : Erwan Richard <contact@erwan-richard.info                   |
 +-----------------------------------------------------------------------+ 
*/

if (!defined('DC_CONTEXT_ADMIN')) { return; }

function defaultIfNotSet($value,$default) {
	if ($value === null)
		return $default;
	else
		return $value;
}


function setSettings() {
	global $core;
	$albums_url_prefix = defaultIfNotSet($core->blog->settings->dcFlickr_albums_url_prefix,'albums');
	$album_url_prefix = defaultIfNotSet($core->blog->settings->dcFlickr_album_url_prefix,'album');
	$default_theme = defaultIfNotSet($core->blog->settings->dcFlickr_default_theme,'default');
	$api_key = defaultIfNotSet($core->blog->settings->dcFlickr_api_key,'ba9ea2d860c0d6352bcf49467c55c283');
	$secret = defaultIfNotSet($core->blog->settings->dcFlickr_secret,'4594858bd60f356a');
	$photos_per_page = defaultIfNotSet($core->blog->settings->dcFlickr_photos_per_page,50);
	
	$settings = $core->blog->settings;
	$settings->setNamespace('dcflickr');
	
	$settings->put('dcFlickr_enabled',false,'boolean');
	$settings->put('dcFlickr_api_key',$api_key,'string','Flickr API key');
	$settings->put('dcFlickr_secret',$secret,'string','Flickr API key');
	$settings->put('dcFlickr_albums_url_prefix',$albums_url_prefix,'string','Flickr Albums List URL prefix');
	$settings->put('dcFlickr_album_url_prefix',$album_url_prefix,'string','Flickr Album URL prefix');
	$settings->put('dcFlickr_default_theme',$default_theme,'string','Default theme to use');
	$settings->put('dcFlickr_photos_per_page',$photos_per_page,integer,'Max photo per album page');
	
	$core->blog->triggerBlog();
	http::redirect('plugin.php?p=dcFlickr');
}

// Getting current parameters

$albums_url_prefix = defaultIfNotSet($core->blog->settings->dcFlickr_albums_url_prefix,'albums');
$album_url_prefix = defaultIfNotSet($core->blog->settings->dcFlickr_album_url_prefix,'album');
$default_theme = defaultIfNotSet($core->blog->settings->dcFlickr_default_theme,'default');
$photos_per_page = defaultIfNotSet($core->blog->settings->dcFlickr_photos_per_page,50);
$api_key = defaultIfNotSet($core->blog->settings->dcFlickr_api_key,'ba9ea2d860c0d6352bcf49467c55c283');
$secret = defaultIfNotSet($core->blog->settings->dcFlickr_secret,'4594858bd60f356a'); 
$active = (boolean)$core->blog->settings->dcFlickr_enabled;


// Setting default parameters if missing configuration
if (is_null($core->blog->settings->dcFlickr_enabled)) {
	try {
		setSettings();
	}
	catch (Exception $e) {
		$core->error->add($e->getMessage());
	}
}

// Saving new configuration
if (!empty($_POST['saveconfig'])) {
	try
	{
		
		$settings = $core->blog->settings;
		$settings->setNamespace('dcflickr');
		
		$active = (empty($_POST['active']))?false:true;
		$photos_per_page = !empty($_POST['photos_per_page'])?$_POST['photos_per_page']:$photos_per_page;
		$albums_url_prefix = !empty($_POST['albums_url_prefix'])?$_POST['albums_url_prefix']:$albums_url_prefix;
		$album_url_prefix = !empty($_POST['album_url_prefix'])?$_POST['album_url_prefix']:$album_url_prefix;
		$default_theme = !empty($_POST['default_theme'])?$_POST['default_theme']:$default_theme;
		
		$settings->put('dcFlickr_enabled',$active,'boolean');
		$settings->put('dcFlickr_albums_url_prefix',$albums_url_prefix,'string','Flickr Albums List URL prefix');
		$settings->put('dcFlickr_album_url_prefix',$album_url_prefix,'string','Flickr Album URL prefix');
		$settings->put('dcFlickr_default_theme',$default_theme,'string','Default theme to use');
		$settings->put('dcFlickr_photos_per_page',$photos_per_page,integer,'Max photo per album page');
		$core->blog->triggerBlog();
		
		$msg = __('Configuration successfully updated.');
	}
	catch (Exception $e)
	{
		$core->error->add($e->getMessage());
	}
}

if (!empty($_POST['dropconnection'])) {
	try
	{
		$core->blog->settings->setNameSpace('dcflickr');
		$core->blog->settings->drop('dcFlickr_user_token');
		$core->blog->settings->drop('dcFlickr_username');
		$core->blog->triggerBlog();
		http::redirect('plugin.php?p=dcFlickr');
		
	}
	catch (Exception $e)
	{
		$core->error->add($e->getMessage());
	}
}



if (is_null($core->blog->settings->dcFlickr_user_token)) {
	$api = new phpFlickr ($api_key,$secret);		
	
	if (isset($_SESSION['dcFlickr_frob'])):
		$token = $api->auth_getToken($_SESSION['dcFlickr_frob']);		
		if ($token === false):
			unset($_SESSION['dcFlickr_frob']);
			$frob = $api->auth_getFrob();
			$api_sig = md5($secret.'api_key'.$api_key.'frob'.$frob.'permsread');
			$flickr_url = "http://www.flickr.com/services/auth/?api_key=$api_key&perms=read&frob=$frob&api_sig=$api_sig";
			$_SESSION['dcFlickr_frob'] = $frob;
		else :
			$core->blog->settings->setNamespace('dcflickr');;
			$core->blog->settings->put('dcFlickr_user_token',$token['token'],'string','dcFlickr User token');
			$core->blog->settings->put('dcFlickr_username',$token['user']['username'],'string','dcFlickr User Name');
			$core->blog->triggerBlog();
			http::redirect('plugin.php?p=dcFlickr');
		endif ;
	else:
		$frob = $api->auth_getFrob();
		$api_sig = md5($secret.'api_key'.$api_key.'frob'.$frob.'permsread');
		$flickr_url = "http://www.flickr.com/services/auth/?api_key=$api_key&perms=read&frob=$frob&api_sig=$api_sig";
		$_SESSION['dcFlickr_frob'] = $frob;
		
	endif;	
}




?>
<html>
<head>
  <title>dcFlickr</title>
  <?php echo dcPage::jsToolMan(); ?>
  <?php echo dcPage::jsConfirmClose('classes-form','add-class-form'); ?>
  <?php echo dcPage::jsPageTabs($default_tab); ?>
</head>

<body>
<h2 style="padding:8px 0 8px 34px;background:url(index.php?pf=dcFlickr/icon_32.png) no-repeat;">
<?php echo html::escapeHTML($core->blog->name); ?> &gt; dcFlickr</h2>

<?php

if (!empty($msg)) {
		echo '<p class="message">'.$msg.'</p>';
}

?>

<div class="multi-part" title="<?php echo __('dcFlickr'); ?>">
<?php
	echo '<form method="post" action="plugin.php">';
	echo '<fieldset><legend>'.__('Plugin activation').'</legend>';
	echo 
	'<p class="field">'.
			form::checkbox('active', 1, $active).
			'<label class=" classic" for="active">'.__('Enable dcFlickr').'</label></p></fieldset>';
	echo 
	'<p><input type="hidden" name="p" value="dcFlickr" />'.
		$core->formNonce().
		'<input type="submit" name="saveconfig" accesskey="s" value="'.__('Save').' (s)"/>';
	echo '</p>';
?>
</div>

<?php if ($active):?>
<fieldset><legend><?= __('Plugin configuration'); ?></legend>

<p class="field">
	<label class=" classic"><?=  __('Albums URL prefix');?> :</label> 
	<?=form::field('albums_url_prefix', 60, 255, $albums_url_prefix);?>
</p>
<p class="field">
	<label class=" classic"><?=  __('Album URL prefix');?> :</label> 
	<?=form::field('album_url_prefix', 60, 255, $album_url_prefix);?>
</p>
<p class="field">
	<label class=" classic"><?=  __('Default theme');?> :</label> 
	<?=form::field('default_theme', 60, 255, $default_theme);?>
</p>
<p class="field">
	<label class=" classic"><?=  __('Photos per album page');?> :</label> 
	<?=form::field('photos_per_page', 60, 255, $photos_per_page);?>
</p>
</fieldset>
<p>
	<input type="hidden" name="p" value="dcFlickr" />
	<?php $core->formNonce(); ?>
	<input type="submit" name="saveconfig" accesskey="s" value="<?= __('Save Configuration').' (s)';?>"/>
</p>

<fieldset><legend><?= __('Plugin Flickr link'); ?></legend>
<?php 
if (is_null($core->blog->settings->dcFlickr_user_token)):
	echo '<p class="error">'. __('dcFlickr is not linked to a Flickr account').'</p>';
	echo '<a target="_blank" href="'. $flickr_url.'">'. __('Flickr connect').'</a>';
	echo '</fieldset>';
else:
	echo '<p>'. __('Connected as ').$core->blog->settings->dcFlickr_username.'</p>'.
	     '<input type="hidden" name="p" value="dcFlickr" />'.
		 $core->formNonce().
		'</fieldset>'. 
		 '<input type="submit" name="dropconnection" accesskey="d" value="'. __('Drop connection').' (d)"/>';
endif; ?>



<?php endif; ?>
</form>
</body></html>